package platform.common.bo;

import java.util.HashMap;
import java.util.Map;

import platform.common.dao.IDomainProviderSet;
import platform.common.dao.impl.CriticalTechnicalDaoException;
import platform.common.dao.impl.DaoConfigException;
import platform.common.dao.impl.DaoException;
import platform.common.dao.impl.TechnicalDaoException;
import platform.common.exception.BusinessException;
import platform.common.exception.CriticalTechnicalException;
import platform.common.exception.InValidDataException;
import platform.common.exception.TechnicalException;

import platform.common.bo.IPersistenceBO;
import platform.common.bo.IdentifiableObj;

import com.google.gson.Gson;

public class PersistenceBO extends IdentifiableObj implements IPersistenceBO {

	/**
	 *
	 * <br/>方法描述:保存前的验证
	 * @param domainProviderSet 业务领域提供者集合
	 * @return boolean
	 */
	protected Map checkBeforeSave(IDomainProviderSet domainProviderSet) throws BusinessException{
		return new HashMap();
	}

	/* (non-Javadoc)
	 * @see platform.common.bo.IPersistenceBO#store(platform.common.dao.IDomainProviderSet)
	 */
	public void store(IDomainProviderSet domainProviderSet) throws BusinessException {
		Map checkMap=checkBeforeSave(domainProviderSet);
		if(checkMap.size()==0){
			//进行保存操作
			try {
				domainProviderSet.getDao().saveOrUpdate(this);
			} catch (Exception e) {
				throw this.handleBusinessBOException("进行保存操作失败。", e);
			}
		}else{
			//else说明校验没有通过,不能进行保存操作,提示信息到action设置
			Gson gson = new Gson();
			throw new InValidDataException(gson.toJson(checkMap));
		}
	}

	/* (non-Javadoc)
	 * @see platform.common.bo.IPersistenceBO#del(platform.common.dao.IDomainProviderSet)
	 */
	public void del(IDomainProviderSet domainProviderSet) throws BusinessException {
			try {
				domainProviderSet.getDao().delete(this);
			} catch (Exception e) {
				throw this.handleBusinessBOException("进行删除操作失败。", e);
			}
	}

	/***
	 * 
	 * <br/>方法描述：处理业务领域层异常的通用方法
	 * @param e
	 * @param errMsg 写日志留用
	 * @return
	 */
	public BusinessException handleBusinessBOException(String errMsg, Exception e) {
		if(e instanceof CriticalTechnicalDaoException) {
			return new CriticalTechnicalException(e);
		} else if(e instanceof TechnicalDaoException) {
			return new TechnicalException(e);
		} else if(e instanceof DaoConfigException) {
			return new CriticalTechnicalException(e);
		} else if(e instanceof DaoException) {
			return new TechnicalException(e);
		} else if(e instanceof BusinessException) {
			return (BusinessException) e;
		} else {
			return new BusinessException(errMsg, e);
		}
	}

	/***
	 * 
	 * <br/>方法描述：处理业务领域层异常的通用方法
	 * @param e
	 * @return
	 */
	public BusinessException handleBusinessBOException(Exception e) {
		return this.handleBusinessBOException("", e);
	}
	

}
